﻿#include "link.h"
#include "entity.h"

Link::Link(Entity *fromEntity, Entity *toEntity)
{
    m_pFromEntity = fromEntity;
    m_pToEntity = toEntity;

    m_pFromEntity->addLink(this);
    m_pToEntity->addLink(this);

    setFlags(QGraphicsItem::ItemIsSelectable);
    setZValue(-1);

    setColor(Qt::darkRed);
    trackNodes();
}

Link::~Link()
{
    m_pFromEntity->removeLink(this);
    m_pToEntity->removeLink(this);
}

Entity *Link::fromEntity() const
{
    return m_pFromEntity;
}

Entity *Link::toEntity() const
{
    return m_pToEntity;
}

void Link::setColor(const QColor &color)
{
    setPen(QPen(color, 1.0));
}

QColor Link::color() const
{
    return pen().color();
}

void Link::trackNodes()
{
    setLine(QLineF(m_pFromEntity->pos(), m_pToEntity->pos()));
}
